package bocchi.general;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

public class DataLoader {
    private static final Map<String, CardData> cardDataMap = new HashMap<>();
    private static final Gson gson = new Gson();

    static {
        Type type = new TypeToken<Map<String, JsonObject>>() {}.getType();
        for (String file : ResourcePath.list(ResourcePath.CARD_DATA)) {
            if (file.endsWith(".json")) {
                Map<String, JsonObject> cardDataJsonMap = gson.fromJson(ResourceLoader.read(file), type);
                cardDataJsonMap.forEach((key, value) -> cardDataMap.put(key, new CardData(key, value)));
            }
        }
    }

    public static CardData getCardData(String raw_id) {
        return cardDataMap.get(raw_id);
    }

    public static Map<String, CardData> getCardDataMap() {
        return cardDataMap;
    }
}
